package com.maxd.modules.redPacket.dao;

import com.maxd.modules.redPacket.entity.RedPacket;
import com.maxd.modules.redPacket.entity.RedPacketDetails;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface RedPacketDetailsRepository extends Repository<RedPacketDetails, Long> {


    @Query(value = "select d.status,d.user_id as userId,d.money as money,u.image_url as imageUrl,ifnull(u.nick_name,'') as nickName,u.phone from red_packet_details d " +
            "left join user_info u on u.id=d.user_id " +
            "where  d.red_packet_id =:redPacketId order by d.create_time desc ",
            countQuery = "select count(*) from red_packet_details d " +
                    "left join user_info u on u.id=d.user_id " +
                    "where d.red_packet_id =:redPacketId "
            ,nativeQuery = true)
    Page<Map<String,Object>> selectRedPacketDetailsList(Pageable pageable, @Param("redPacketId") Long redPacketId);


    @Query(value = "select count(*) from red_packet_details where user_id=:userId and red_packet_id =:redPacketId ",nativeQuery = true)
    int selectRedPacketDetailsByUserIdAndRedPackId(@Param("userId") Long userId, @Param("redPacketId") Long redPacketId);

    @Query(value = "select count(*) from red_packet_details where user_id=:userId and date_format(create_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') ",nativeQuery = true)
    int selectRedPacketDetailsByUserIdAndDate(@Param("userId") Long userId);

    @Query(value = "from RedPacketDetails s where s.userId=:userId and s.status=1")
    List<RedPacketDetails> selectRedPacketDetailsListByUserId(@Param("userId") Long userId);



}